package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("/cargo/contract")
@Slf4j
public class ContractController extends BaseController {

    @Reference
    private ContractService contractService;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum ,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        //构造条件对象
         ContractExample contractExample = new ContractExample();
         //根据create_time降序排序
        contractExample.setOrderByClause("create_time desc");
        // 获取条件的集合，获取Criteria对象来设置查询条件
        ContractExample.Criteria criteria = contractExample.createCriteria();
        //设置查询条件
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        /**
         * 登陆用户的degree
         *   2-管理所有下属部门和人员
         *   3-管理本部门
         *   4-普通员工
         */
        //细粒度权限控制显示数据
        User loginUser = getLoginUser();
         Integer degree = loginUser.getDegree();
        PageInfo<Contract> pageInfo = null;
         //判断用户等级
        if(degree==4){
            //A.登陆用户的degree=4,说明是普通用户，只能查看自己创建的购销合同
            //SELECT * FROM co_contract WHERE create_by='登陆用户的id'
            criteria.andCreateByEqualTo(loginUser.getId());
            pageInfo = contractService.findByPage(contractExample, pageNum, pageSize);
        }else if(degree==3){
            //B.登陆用户的degree=3,说明是部门经理，可以查看本部门下的所有购销合同
            //SELECT * FROM co_contract WHERE create_dept='登陆用户的部门id'
            criteria.andCreateDeptEqualTo(loginUser.getId());
            pageInfo = contractService.findByPage(contractExample, pageNum, pageSize);
        } else if (degree == 2){
            //C.登陆用户的degree=2,说明是大部门经理，可以查看本部门及其所有子部门创建的购销合同
            //SELECT * FROM co_contract WHERE FIND_IN_SET(create_dept,getDeptChild('100'))
            pageInfo = contractService.findByParentDept(loginUser.getDeptId(), pageNum, pageSize);
            pageInfo = contractService.findByParentDept(loginUser.getDeptId(), pageNum, pageSize);
        }

        request.setAttribute("pageInfo", pageInfo);
        return "cargo/contract/contract-list";
    }

    /**
     * 进入到添加页面
     * @param model
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        return "cargo/contract/contract-add";
    }

    @RequestMapping("/edit")
    public String edit(Contract contract){
        // 设置企业id、企业名称（后期再从登陆购销合同哪里获取）
        contract.setCompanyId(getLoginCompanyId());
        contract.setCompanyName(getLoginCompanyName());

        //从前端封装的contract对象获取id
        //根据id判断是添加还是修改
        if(StringUtils.isEmpty(contract.getId())){
            //设置创建时间
            contract.setCreateTime(new Date());
            //创建人。括号内方法在BaseController里面
            contract.setCreateBy(getLoginUser().getId());
            //创建人所属部门
            contract.setCreateDept(getLoginUser().getDeptId());
            contractService.save(contract);
        }else {
            //修改
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list";
    }


    /**
     * 4. 列表页面，点击编辑，进入contract-update.jsp页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,Model model){
        //根据购销合同id查询
         Contract contract = contractService.findById(id);
        //保存查询结果
        model.addAttribute("contract",contract);
        return "cargo/contract/contract-update";
    }

    /**
     * 5. 删除
     */
    @RequestMapping("delete")
    public String delete(String id){
        contractService.delete(id);
        return "redirect:/cargo/contract/list";
    }

    /**
     * 查看
     */
    @RequestMapping("/toView")
    public String toView(String id){
        //根据购销合同查询
         Contract contract = contractService.findById(id);
         //保存查询结果
        request.setAttribute("contract",contract);
        return "cargo/contract/contract-view";
    }

    /**
     * 提交
     */
    @RequestMapping("/submit")
    public String submit(String id){
        //根据
         Contract contract = new Contract();
         //因为不能之间传入id到update方法中，所以需要在contract设置id
         contract.setId(id);
         contract.setState(1);
        contractService.update(contract);
        return "redirect:/cargo/contract/list";
    }

    /**
     * 取消
     */
    @RequestMapping("/cancel")
    public String cancel(String id){
// 提交：修改购销合同状态为0
         Contract contract = new Contract();
         contract.setId(id);
         contract.setState(0);
        contractService.update(contract);
        return "redirect:/cargo/contract/list";
    }
}
